package com.pomelo.tiku.receivers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.pomelo.tiku.events.NetWorkChangeEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * @author liuxiangwang
 * @date 2022/06/07.
 */
public class NetWorkChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable()) {
            EventBus.getDefault().post(new NetWorkChangeEvent(true));
        } else {
            EventBus.getDefault().post(new NetWorkChangeEvent(false));
        }
    }
}
